package page.sample.administratorpackage;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import page.sample.AuthorizationPage;
import dao.impl.AdministratorDao;
import dao.impl.UserDao;
import entity.Administrator;
import entity.User;

public class BlackListPage extends WebPage {
	
	public BlackListPage(final Administrator administrator)
	{
		final UserDao userDao = new UserDao(
				app.MyFirstWebApplication.DATA_XML_FOLDER);
		
		final AdministratorDao administratorDao = new AdministratorDao(
				app.MyFirstWebApplication.DATA_XML_FOLDER);

		add(new DataView<User>("table-rows", new ListDataProvider<User>(
				administratorDao.get("admin").getBlackList())) {
			@Override
			protected void populateItem(final Item<User> item) {
				final User user = item.getModelObject();
				// add data for table cell in row
				item.add(new Label("login", user.getLogin()));
				item.add(new Label("password", user.getPassword()));
				item.add(new Label("name", user.getName()));
				item.add(new Label("lastname", user.getLastname()));

				// add button component to open "edit" page
				item.add(new Link("deleteBlackList") {
					@Override
					public void onClick() {

						for (final User row : administrator.getBlackList())
						{
							if (row.getLogin().equals(user.getLogin()))
							{
								row.setAccess(true);
								userDao.update(row);
								administrator.getBlackList().remove(row);
								break;
							}
						}
						
						administratorDao.update(administrator);
						setResponsePage(new BlackListPage(administrator));
					}
				});
			}
		});
		
		add(new Link("showUsers")
		{

			@Override
			public void onClick() {
				setResponsePage(new ShowUsers(administrator));
			}
			
		});
		
		add(new Link("addGoods")
		{

			@Override
			public void onClick() {
				setResponsePage(new AddGoodsPage(administrator));
				
			}
			
		});
		
		add(new Link("showGoods")
		{

			@Override
			public void onClick() {
				setResponsePage(new ShowGoodsPage(administrator));
				
			}
			
		});
		
		add(new Link("showBlackList")
		{

			@Override
			public void onClick() {
				setResponsePage(new BlackListPage(administrator));
			}
			
		});
		
		add(new Link("exit")
		{

			@Override
			public void onClick() {
				setResponsePage(new AuthorizationPage());
				
			}
			
		});
	}

}
